package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class Del implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要删除的书名：");
        String name = scanner.nextLine();
        int ret = bookList.getUsedSize();
        int pos = -1;
        for (int i = 0; i < ret; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                pos = i;
                break;
            }
        }
        if(pos == -1){
            System.out.println("没有该书！");
            return;
        }
        for (int i = pos; i < ret - 1; i++) {
            Book book = bookList.getBook(i + 1);
            bookList.setBooks(i,book);
        }
        bookList.setUsedSize(ret - 1);
        System.out.println("删除成功！");
    }
}
